package com.example;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.multidex.MultiDex;


import com.example.app.ActivityStackManager;
import com.example.baselibrary.util.LKToastUtil;
import com.example.baselibrary.util.router.RouterActivityIntentResourceKey;
import com.example.baselibrary.util.router.RouterConfig;
import com.example.baselibrary.util.router.RouterTransferCenterUtil;
import com.example.im.IMInit;

import cc.vv.lkbasecomponent.base.app.LKBaseApplication;
import cc.vv.lkimcomponent.lkim.inter.LKIMOperateInter;
import cc.vv.lklibrary.log.LogOperate;

/**
 * Created by sun.li on 2018/6/6.
 *
 * @author sun.li
 */

public class BaseNewApplication extends LKBaseApplication{
    private static BaseNewApplication mApplication;

    /**
     * 全局Handler
     */
    public Handler mHandler ;
    @Override
    public void onCreate() {
        super.onCreate();
        RouterConfig.getInstance().initRouter();
        mApplication = this;
        mHandler =new  Handler();
        //初始化LeakCanary
//        if (LeakCanary.isInAnalyzerProcess(this)) {
//            return;
//        }
//        LeakCanary.install(this);
    }
    public static BaseNewApplication getInstance() {
        return mApplication;
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    /**
     * 添加activity到容器中
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
            ActivityStackManager.getInstance().addActivity(activity);
    }

    /**
     * 添加activity到容器中
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        ActivityStackManager.getInstance().remove(activity);
    }


    /**
     * 遍历指定的Activity并finish
     */
    public void exit(int num) {
        ActivityStackManager.getInstance().exit(num);
    }




    /**
     * 退出登录
     *
     * @param context
     */
    public void loginOut(final Context context) {
        if (context==null){
            return;
        }
        final Context fContext = context;
        IMInit.Companion.getInstance().exit(new LKIMOperateInter() {
            @Override
            public void onSuccess() {
                super.onSuccess();
                //退出登录
                ActivityStackManager.getInstance().exitApp();
                Intent intent = RouterTransferCenterUtil.getInstance().getRouterIntent(fContext,
                        RouterActivityIntentResourceKey.KEY_BT_LOGIN_LOGIN_ACTIVITY);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                LogOperate.e("登录========"+intent);
                RouterTransferCenterUtil.getInstance().routerStartActivity(fContext, intent);
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
                LKToastUtil.showToastShort(message);
                LogOperate.e(message);
            }
        });
    }
}
